package com.wandian.admin;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Vector;

import com.wandian.command.AdminCommand;
import com.wandian.command.CommandSet;
import com.wandian.util.Log;

/**
 * 管理员远程维护管理服务器
 * 
 * @author long
 * @Create 2012-10-16 上午11:42:59
 * @QQ 108232706
 * @version 1.0
 * @description AdminCmdMgr
 */
public class AdminCmdMgr extends Thread
{
	private int id;
	private List<Requestor> requestors;
	private ServerSocket serverSocket;
	private boolean isRunning;

	public AdminCmdMgr(int port)
	{
		super("Thread-AdminCmdMgr-" + port);
		this.id = 0;
		try {
			this.serverSocket = new ServerSocket(port);
			this.isRunning = true;
			if (this.requestors == null) {
				this.requestors = new Vector<Requestor>();
			}
		} catch (IOException e) {
			Log.error("", e);
		}
	}

	@Override
	public void run()
	{
		try {
			while (this.isRunning) {
				Socket socket = this.serverSocket.accept();
				this.id += 1;
				Requestor requestor = new Requestor(socket, this.id);
				requestor.start();
				this.requestors.add(requestor);
			}
		} catch (IOException localIOException) {
		}
	}

	public void stopping() {
		try {
			this.isRunning = false;
			if (this.requestors != null) {
				for (Requestor requestor : this.requestors) {
					requestor.stopping();
				}
			}
			this.serverSocket.close();
		} catch (IOException e) {
			Log.error("", e);
		}
	}

	class Requestor extends Thread {
		private final Socket socket;
		private final BufferedReader reader;
		private final BufferedWriter writer;

		public Requestor(Socket socket, int id) throws UnsupportedEncodingException, IOException {
			super("Thread-Admin-Requestor-" + id);
			this.socket = socket;
			this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
			this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
		}

		@Override
		public void run()
		{
			try {
				send("connect successfully, welcome admin... type quit or exit to exit");
				String line;
				while ((line = this.reader.readLine()) != null)
				{
					String[] params = line.split(" ");
					AdminCommand adminCommand = CommandSet.getCommand(params[0]);
					if (adminCommand != null)
						adminCommand.execute(params, this.writer);
					else
						send("command not found, you can use help list all command.");
				}
			} catch (IOException e) {
				try {
					this.socket.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}

		public void send(String str) throws IOException {
			this.writer.write(str);
			this.writer.newLine();
			this.writer.flush();
		}

		public void stopping() throws IOException {
			this.socket.close();
		}
	}
}